package com.usion.login.service.impl;


import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.usian.common.ResultVo;
import com.usion.login.dao.LogDao;
import com.usion.login.dao.UserDao;
import com.usion.login.pojo.User;
import com.usion.login.service.Logservice;
import lombok.SneakyThrows;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LogserviceImpl implements Logservice {
@Autowired
private LogDao logDao;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private UserDao userDao;
@Autowired
private RabbitTemplate rabbitTemplate;

    @SneakyThrows
    @Override
    public ResultVo code(String mobile) {
           Boolean isPhone= Validator.isMobile(mobile);
           if (isPhone==true){

             int count=logDao.seavCode(mobile);
             if (count>0){
                 rabbitTemplate.convertAndSend("mm", "pp", mobile);

                 return new ResultVo(200,"发送成功",null);
             }else {
              return new ResultVo(201,"该手机号未注册",null);
             }
           }else {
              return new ResultVo(201,"手机号错误",null);
           }


    }

    @Override
    public ResultVo denlu(String phone, String code) {
//        判断手机号密码是否为空
        Boolean empty = StrUtil.isEmpty(phone);
        Boolean code1 = StringUtils.isEmpty(code);
        if (!empty){
        if (!code1){
//        判断验证码是否正确
            String s = stringRedisTemplate.opsForValue().get(phone);
            if (!StringUtils.isEmpty(s)){
                if (s.equals(code)){
//        用户是否存在
                   User user=logDao.selOen(phone);
                   if (user!=null){
                       return new ResultVo(200,"成功",user);
                   }else {
                       return new ResultVo(201,"用户不存在",null);
                   }

//        验证码是否正确
//        返回个人信息
                }else {
                    return new ResultVo(201,"验证码错误",null);
                }
            }else {
                return new ResultVo(201,"验证码超时",null);
            }

        }else {
            return new ResultVo(201,"验证码不许为空",null);
        }
        }else {
            return new ResultVo(201,"手机号不许为空",null);
        }

    }

    @Override
    public ResultVo add(User user) {
        userDao.insert(user);
        return new ResultVo();
    }
}
